clear all; close all;

%Temporarily add to path (remember to call path at the end of script to
%restore)
p = path;
path(p,'C:\Users\mypc\Documents\MATLAB\Option Object Oriented Framework')

% SEQUENCE EXCHANGE BOARD TIME PAPER BID BID-DEPTH BID-DEPTH-TOTAL BID-NUMBER OFFER OFFER-DEPTH
% OFFER-DEPTH-TOTAL OFFER-NUMBER OPEN HIGH LOW LAST CHANGE CHANGE-PERCENT VOLUME VALUE TRADES STATUS QUOTE-ID
%Some tests for parsing html strings from primetrader
filenameToLoad2 = 'C:\Users\mypc\Documents\MATLAB\Option Object Oriented Framework\Data\renter_nb.xlsx';
[num2,txt2,raw2]  = xlsread( filenameToLoad2 );


tolerance = 1e-6;
N = 7;

tic; 
ticker_letters = 'OBX';
year_letter = num2str( year(now)-2010 );
Call_Letter = char( month(now)+1+64 );
Put_Letter = char( month(now)+1+76 );
S = getDoubleFromUrl( 'http://localhost:8678/quotes/OBX.OSE', 17 ); %Get last trade
T = getDecimalExpiration( strcat([ticker_letters year_letter Call_Letter]) );
q = 0;
yieldCurve = YieldCurve( raw2 );
% r = yieldCurve.getSpotRate( T, datestr(now-20) );
r = 0.024;

call_string = '';
put_string = '';
ATM_strike = S-mod(S,5);
K = ATM_strike-5*N:5:ATM_strike+5*N;
for i = 1:length(K)
    call_string = strcat( [call_string ticker_letters year_letter Call_Letter num2str(K(i)) '.OMFE&'] );
    put_string = strcat( [put_string ticker_letters year_letter Put_Letter num2str(K(i)) '.OMFE&'] );
end

call_data = urlread( strcat(['http://localhost:8678/quotes/' call_string]) );
put_data = urlread( strcat(['http://localhost:8678/quotes/' put_string]) );

call_prices = getDoublesFromUrl( call_data, [6 10 17] )
put_prices = getDoublesFromUrl( put_data, [6 10 17] )
    [row_ind col_ind] = find( put_prices<0.00001 );
    put_prices( row_ind, col_ind ) = NaN; 
    
ivols_c = [blsImpVolCall( S*ones(length(K),1), K.', r, T, call_prices(:,1) , q, tolerance )...
    blsImpVolCall( S*ones(length(K),1), K.', r, T, call_prices(:,2) , q, tolerance )...
    blsImpVolCall( S*ones(length(K),1), K.', r, T, mean(call_prices(:,1:2),2) , q, tolerance)...
    blsImpVolCall( S*ones(length(K),1), K.', r, T, call_prices(:,3) , q, tolerance)];

ivols_p = [blsImpVolPut( S*ones(length(K),1), K.', r, T, put_prices(:,1) , q, tolerance )...
    blsImpVolPut( S*ones(length(K),1), K.', r, T, put_prices(:,2) , q, tolerance )...
    blsImpVolPut( S*ones(length(K),1), K.', r, T, mean(put_prices(:,1:2),2) , q, tolerance )...
    blsImpVolPut( S*ones(length(K),1), K.', r, T, put_prices(:,3) , q, tolerance )];

toc

figure()
plot( K, ivols_c(:,1),'b.-' )
hold on
plot( K, ivols_c(:,2),'b.-' )
plot( K, ivols_p(:,1),'r.-' )
plot( K, ivols_p(:,2),'r.-' )

figure()
plot( K, ivols_c(:,3),'b.-' )
hold on
plot( K, ivols_p(:,3),'r.-' )
plot( K, ivols_c(:,1),'b:' )
plot( K, ivols_c(:,2),'b:' )
plot( K, ivols_p(:,1),'r:' )
plot( K, ivols_p(:,2),'r:' )
plot( K, ivols_c(:,4),'b*' )
plot( K, ivols_p(:,4),'r*' )
xlabel('Strike')
ylabel('Implied Vol.')
legend('Call Mid','Put Mid','C Bid', 'C Offer', 'P Bid', 'P Offer')
legend('boxoff')


path(p)